//
//  SignupViewController.m
//  VICKY ZONE
//
//  Created by sunx on 15/4/5.
//  Copyright (c) 2015年 sunx. All rights reserved.
//
#define SERVER @"http://www.samoyun.com/controller/signupController.php"
#define TEST @"http://127.0.0.1/01/controller/signupController.php"
#import "NSURLSession+KKAddition.h"
#import "SignupViewController.h"
#import "SVProgressHUD.h"
@interface SignupViewController ()

@end

@implementation SignupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)submit:(id)sender {
    if(![self.emailTextField.text isEqualToString:@""]&&
       ![self.usernameTextField.text isEqualToString:@""]&&
       ![self.realnameTextField.text isEqualToString:@""]&&
       ![self.passwordTextField.text isEqualToString:@""]) {
        [SVProgressHUD show];
        
        //NSLog(@"%@\n%@\n%@\n%@\n",self.emailTextField.text,
        //      self.usernameTextField.text,
        //      self.realnameTextField.text,
        //      self.passwordTextField.text);
        
        [NSURLSession POST:SERVER
                   pararms:@{
                             @"email" : self.emailTextField.text,
                             @"username" : self.usernameTextField.text,
                             @"realname":self.realnameTextField.text,
                             @"password":self.passwordTextField.text} completed:^(NSData *data) {
                                 NSString *result =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                 NSLog(@"result :%@",result);
                                 if([result isEqualToString:@"exist"]) {
                                     [self performSelectorOnMainThread:@selector(userExist) withObject:nil waitUntilDone:YES];
                                 }
                                 if([result isEqualToString:@"success"]) {
                                     [self performSelectorOnMainThread:@selector(signupSuccess) withObject:nil waitUntilDone:YES];
                                 }
                                 if([result isEqualToString:@"closed"]) {
                                     [self performSelectorOnMainThread:@selector(signUpClosed) withObject:nil waitUntilDone:YES];
                                 }
                                 NSLog(@"%@",result);
                             } failed:^(NSError *error) {
                                 if(!error) {
                                     NSLog(@"%@",error);
                                 }
                                 [self performSelectorOnMainThread:@selector(netError) withObject:self waitUntilDone:YES];
                             }];
    }else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请填写完整的信息" message:@"" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
        [alert show];
    }
}
- (void)userExist {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"注册失败" message:@"该用户已经存在请重新注册" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
    self.usernameTextField.text = @"";
    self.emailTextField.text = @"";
    [alert show];
    [SVProgressHUD dismiss];
    
}
- (void)signupSuccess {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"注册成功" message:@"欢迎使用VICKY ZONE" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
    [alert show];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setValue:self.usernameTextField.text forKey:@"username"];
    
    NSLog(@"%@",self.usernameTextField.text);
    
    [SVProgressHUD dismiss];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)signUpClosed {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"注册已经关闭" message:@"暂时不开放注册，敬请原谅" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
    [alert show];
    [SVProgressHUD dismiss];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)netError {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提交失败" message:@"请检查网络是否连接" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
    [alert show];
    [SVProgressHUD dismiss];
}
- (IBAction)emailFinished:(id)sender {
    if([self isEmail:self.emailTextField.text] ||
       [self.emailTextField.text isEqualToString:@""]) {
        NSLog(@"yes");
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"邮箱格式错误" message:@"请重新输入" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        self.emailTextField.text = @"";
        [alert show];
        
    }
}
- (IBAction)usernameFinished:(id)sender {
    if([self isUsername:self.usernameTextField.text]||
       [self.usernameTextField.text isEqualToString:@""]) {
        NSLog(@"yes");
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"用户名错误" message:@"用户名只能是2-10位的英文或数字" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        self.usernameTextField.text = @"";
        [alert show];
        
    }
}

- (IBAction)realnameFinished:(id)sender {
    if([self isRealname:self.realnameTextField.text] ||
       [self.realnameTextField.text isEqualToString:@""]) {
        NSLog(@"yes");
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请填写真实姓名" message:@"" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        self.realnameTextField.text = @"";
        [alert show];
        
    }
}

- (IBAction)passwordFinished:(id)sender {
    if([self isPassword:self.passwordTextField.text]||
       [self.passwordTextField.text isEqualToString:@""]) {
        NSLog(@"yes");
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"密码格式错误" message:@"密码应该为数字或字母，长度6-12位" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        self.passwordTextField.text = @"";
        [alert show];
        
    }
}
-(BOOL) isEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
-(BOOL) isUsername:(NSString *)username {
    NSString *emailRegex = @"[a-zA-Z0-9]{2,10}$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:username];
}
-(BOOL) isRealname:(NSString *)realname {
    NSString *emailRegex = @"[\u4e00-\u9fa5 a-zA-Z0-9]{2,15}$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:realname];
}
-(BOOL) isPassword:(NSString *)password {
    NSString *emailRegex = @"[a-zA-Z0-9]{6,15}$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:password];
}
@end
